var timeFormatMap = {
    'year': "%Y-%m",
    'month': "%Y-%m-%d",
    'day': "%Y-%m-%d %H"
};
var resultMap = {
    'totalNameSql': ["工厂水表","工厂电表","工厂燃气表","工厂冷量表","工厂蒸汽表","工厂压缩空气表"],
    'totalName': ["water","electricity","gas","heat","steam","airPress"]
};
dev.action = function (resp, param) {
    if (param.time == null || param.workSpace == null || param.id == null) {
        resp.setJsData('必须传入设备ID、时间参数、空间架构参数')
        return
    }
    var time = param.time;
    var workshop = param.workSpace
    var id = param.id
    var timeObj = dtime.parseStringToDateTimeViaPattern(time)

    var times = 0
    var timePattern = ''
    var flag = 0
    // 获取遍历的时间长度
    switch (time.length) {
        case 4:
            times=timeObj.monthOfYear().getMaximumValue();
            timePattern = 'yyyy-MM'
            flag = 1
            break;
        case 7:
            times=timeObj.dayOfMonth().getMaximumValue();
            timePattern = 'yyyy-MM-dd'
            flag = 2
            break;
        case 10:
            times=timeObj.hourOfDay().getMaximumValue() + 1;
            timePattern = 'yyyy-MM-dd HH'
            flag = 3
            break;
        default:
            break;
    }

    var result = iot.list();

    var db = iot.db();
    var totalList = resultMap['totalName'];
    var totalSqlList = resultMap['totalNameSql'];
    var timeType = iot.getTimeByFormat(time);
    var format = timeFormatMap[timeType];
    var resultValue = db.qry("select dev.name,win.dev_id,SUM(win.dvalue) totalSum,DATE_FORMAT(win.the_time,'"+ format +"') timeInterval\n" +
        "from iot_dev_data_win win\n" +
        "LEFT JOIN energy_struc_prop esp\n" +
        "on win.dev_id = esp.dev_id\n" +
        "LEFT JOIN iot_dev dev\n" +
        "on win.dev_id = dev.id\n" +
        "where esp.energy_struc_id = "+ workshop +"\n" +
        "and win.the_time like '"+ time +"%'\n" +
        "and win.data_type = esp.`name`\n" +
        "GROUP BY timeInterval,dev_id\n" +
        "ORDER BY timeInterval");



    //开始遍历数据
    for (var i = 0; i < times; i++) {

        var newItem = {
            water: 0,
            electricity: 0,
            gas: 0,
            heat: 0,
            airPress: 0,
            steam: 0
        }
        newItem.time = timeObj.toString(timePattern);

        for(var ix in totalSqlList){

            var totalItem = totalSqlList[ix];
            var totalItemName = totalList[ix];
            resultValue.forAll(function (item) {
                if(item && item.name && item.name === totalItem && item.timeInterval === newItem.time){
                    switch (totalItemName) {
                        case "water":
                            newItem.water = item.totalSum;
                            break;
                        case "electricity":
                            newItem.electricity = item.totalSum;
                            break;
                        case "gas":
                            newItem.gas = item.totalSum;
                            break;
                        case "heat":
                            newItem.heat = item.totalSum;
                            break;
                        case "airPress":
                            newItem.airPress = item.totalSum;
                            break;
                        case "steam":
                            newItem.steam = item.totalSum;
                            break;
                        default:
                            break;
                    }
                }
            });
        }


        result.add(newItem);
        // 时间递进
        if (flag == 1) {
            timeObj = timeObj.plusMonths(1);
        } else if (flag == 2) {
            timeObj = timeObj.plusDays(1);
        } else {
            timeObj = timeObj.plusHours(1);
        }
    }

    // 求和算累积量，放到最后
    var lastSum = {
        water: 0,
        electricity: 0,
        gas: 0,
        heat: 0,
        airPress: 0,
        steam: 0
    }
    for (var i = 0; i < result.length; i++) {
        for (var key in lastSum) {
            lastSum[key] += parseFloat(result[i][key])
        }
    }
    for (var key in lastSum) {
        lastSum[key] = lastSum[key].toFixed(2)
    }
    lastSum['time'] = '累积量'
    result.add(lastSum)

    var jsData = {
        length: times,
        datas: result
    }
    resp.setJsData(jsData)
}